package h0908;
/**
  * @description 反转链表
  * @author 不知名帅哥
  * @date 2024/9/8 16:38
  * @version 1.0
*/
public class ReverseLinkedList {
    public static void main(String[] args) {

    }
    static class ListNode {
        int val;
        ListNode next;

        public ListNode() {
        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public ListNode reverseList(ListNode head) {
        if (head==null) return null;
        ListNode dummy=new ListNode(-1);
        dummy.next=head;

        ListNode p=head;
        ListNode q=p.next;
        while (q!=null){
            p.next=q.next;
            q.next= dummy.next;
            dummy.next=q;
            q=p.next;
        }
        return dummy.next;
    }
}
